package com.avalon.core.field;

import java.lang.reflect.Type;

import com.avalon.core.face.field.IFieldDefaultValue;
import lombok.Getter;

public class RelatedField extends Field{
    @Getter
    private final String related;

    public RelatedField(Builder builder) {
        super(builder);
        this.related = builder.relatedField;
    }

    public RelatedField(String label, String relatedField) {
        super(label);
        this.related = relatedField;
    }

    @Override
    public Integer getSqlType() {
        return getService().getField(related).getSqlType();
    }

    @Override
    public Type getFieldType() {
        return getService().getField(related).getFieldType();
    }

    @Override
    public Object getSqlValue(Object value) {
        return getService().getField(related).getSqlValue(value);
    }

    @Override
    public String getCreateTableSql() {
        return getService().getField(related).getCreateTableSql();
    }

    @Override
    public String getSampleCreateTableSql() {
        return getService().getField(related).getSampleCreateTableSql();
    }

    public static class Builder extends Field.Builder<Builder> {
        private String relatedField;

        public Builder setRelatedField(String relatedField) {
            this.relatedField = relatedField;
            return this;
        }
        
        public String getRelatedField() {
            return relatedField;
        }

        @Override
        public Builder setIsUnique(Boolean isUnique) {
             this.isUnique = isUnique;
             return this;
        }

        @Override
        public Builder setAllowNull(Boolean allowNull) {
            this.allowNull = allowNull;
            return this;
        }

        @Override
        public Builder setIsRequired(Boolean isRequired) {
            this.isRequired = isRequired;
            return this;
        }

        @Override
        public Builder setDefaultValue(IFieldDefaultValue defaultValue) {
            this.defaultValue = defaultValue;
            return this;
        }

        @Override
        public Builder setIsAutoIncrement(Boolean isAutoIncrement) {
            this.isAutoIncrement = isAutoIncrement;
            return this;
        }

        @Override
        public Builder setIsPrimaryKey(Boolean isPrimaryKey) {
            this.isPrimaryKey = isPrimaryKey;
            return this;
        }

        @Override
        public Builder setIsReadonly(Boolean isReadonly) {
            this.isReadonly = isReadonly;
            return this;
        }

        @Override
        public Builder setFieldName(String fieldName) {
            this.fieldName = fieldName;
            return this;
        }

        @Override
        public Builder setLabel(String label) {
            this.label = label;
            return this;
        }

        @Override
        public Builder setCanSearch(Boolean canSearch) {
            this.canSearch = canSearch;
            return this;
        }

        @Override
        public Field build() {
            return new RelatedField(this);
        }
    }
}
